package node;

import java.util.Stack;

/*
* 给一个头结点 从尾到头返回每个结点的值
* */
public class QuestionOne {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(3);
        head.next.next = new ListNode(2);


        Solution solution = new Solution();
        int[] ints = solution.reversePrint(head);
        for (int anInt : ints) {
            System.out.println(anInt);
        }


    }
}
class Solution {
    public int[] reversePrint(ListNode head) {
       /* int[] result;
        if (head.next == null){
            result = new int[1];
            result[0]= head.val;
            return result;
        }
        ListNode temp =head;
        int count =1;
        while(true){
            if(temp.next == null){
                break;
            }
            count++;
            temp = temp.next;
        }


        result = new int[count];

        //返回节点的数量
        for(int i=count;i>0;i--){
            temp = head;
            for(int j=1; j<i ;j++){
                temp = temp.next;
            }
            result[count -i] = temp.val;
        }

        return result;*/

        Stack<ListNode> stack = new Stack<ListNode>();
        ListNode temp = head;
        while (temp != null) {
            stack.push(temp);
            temp = temp.next;
        }

        int count = stack.size();
        int[] result = new int[count];

        for (int i = 0; i < count; i++) {
            result[i] = stack.pop().val;
        }
        return result;
    }
}
class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }

    public void setNext(ListNode next) {
        this.next = next;
    }
    public ListNode getNext() {
        return next;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }
}
